package org.glut.controller;

//import org.apache.log4j.Logger;

import org.glut.bean.CFAir;
import org.glut.bean.User;
import org.glut.config.MyUserDetils;
import org.glut.service.CFAirService;
import org.glut.service.MessageService;
import org.glut.service.SerialPortMsg;
import org.glut.service.UserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Controller
public class RouterController {
    //    private static Logger logger = Logger.getLogger(RouterController.class);
    @Autowired
    MessageService messageService;
    @Resource
    UserServer userServer;
    @Autowired
    MyUserDetils myUserDetils;

    public  static ExecutorService pool = Executors.newFixedThreadPool(50);

    @RequestMapping({"/index", "/"})
    public String index() {
        return "/index";
    }

    @RequestMapping("/toLogin")
    public String login() {
        return "/myviews/login";
    }

    @RequestMapping("/level1/{id}")
    public String level1(@PathVariable("id") int id) {
        return "views/level1/" + id;
    }

    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id") int id) {
        return "/views/level2/" + id;
    }

    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id") int id) {
        return "/views/level3/" + id;
    }

    @RequestMapping("/level4/{id}")
    public String level4(@PathVariable("id") int id) {
        return "/views/level4/" + id;
    }

    @RequestMapping("/level5/{id}")
    public String level5(@PathVariable("id") int id) {
        return "/views/level5/" + id;
    }

    @RequestMapping("/myIndex")
    public String myIndex() {
        return "/views/myindex";
    }

    @RequestMapping("/admin")
    public String admin() {
        return "/myviews/admin";
    }

    @RequestMapping("/light")
    public String light() {
        return "/myviews/light";
    }

    @RequestMapping("/light2")
    public String light2() {
        return "/myviews/light2";
    }

    @RequestMapping("/getserialMsg")
    @ResponseBody
    public List<String> getserialMsg () {
        ArrayList<String> list = new ArrayList<>(SerialPortMsg.serialMegList);
        SerialPortMsg.serialMegList.clear();
        return list;
    }

    /**
     * 连接wifi
     * @param IPAddress
     * @param wifiport
     * @return
     */
    @RequestMapping("/connwifi")
    public boolean connwifi(String IPAddress, String wifiport) {

        System.out.println("ip:"+IPAddress+"  port:"+wifiport);
        if ( IPAddress != null && wifiport != null) {
            System.out.println("新建立连接");
            Runnable run = new Runnable() {
                @Override
                public void run() {
                    messageService.mesService(IPAddress, Integer.parseInt(wifiport));
                }
            };
            pool.execute(run);
//            messageService.mesService(IPAddress, Integer.parseInt(wifiport));
        }
        return true;
    }

    @RequestMapping("/getwifiStatus")
    @ResponseBody
    public boolean getwifiStatus() {
        try {
            messageService.socket.sendUrgentData(0xFF);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * 获取登录的用户
     *
     * @return
     */
    @RequestMapping("/getUser")
    @ResponseBody
    public User getuser() {
        String username = myUserDetils.getUsername();
        User user = userServer.getUserByname(username);
        return user;
    }

    /**
     * 获取所有的用户
     *
     * @return
     */
    @RequestMapping("/getAllUsers")
    @ResponseBody
    public ArrayList<User> getAllUsers() {
        ArrayList<User> allUsers = userServer.getAllUsers();
        return allUsers;
    }

    /**
     * 删除用户
     *
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public boolean updateUser(User user) {
        return userServer.updateUser(user);
    }

    /**
     * 删除用户
     *
     * @param username
     * @return
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public boolean deleteUserByname(String username) {
        return userServer.deleteUserByname(username);
    }

    /**
     * 注冊
     *
     * @param user
     * @return
     */
    @RequestMapping("/sign")
    @ResponseBody
    public boolean addUser(User user) {
        if (userServer.getUserByname(user.getName()) != null) {
            return false;
        } else {
            return userServer.addUser(user);
        }
    }

    @RequestMapping("/test.json")
    public String getTestJson() {
        return "/test.json";
    }

}
